<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="物料编号" label-width="64px">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请输入物料编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="物料名称" label-width="64px">
          <el-input
            v-model="searchParam.materialName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="操作类型" label-width="64px">
          <el-select
            v-model="searchParam.workKind"
            placeholder="请选择操作类型"
            clearable
          >
            <el-option
              v-for="item in useDictionary('MoldWorkKind')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="产品号" label-width="64px">
          <el-input
            v-model="searchParam.productCode"
            placeholder="请输入产品号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item> -->
        <el-form-item
          v-if="route.query.mod"
          label="模具编码"
          label-width="64px"
        >
          <el-input
            v-model="searchParam.mouldCode"
            placeholder="请输入模具编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="操作时间" label-width="64px">
          <el-date-picker
            v-model="searchParam.workTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getworkTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <!-- <template #header> </template> -->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        @sort-change="onTableSortChanged"
      >
        <el-table-column
          label="物料号"
          prop="materialCode"
          min-width="100"
          show-overflow-tooltip
          sortable
        />
        <el-table-column
          label="物料名称"
          prop="materialName"
          min-width="100"
          show-overflow-tooltip
          sortable
        />
        <el-table-column
          label="模具编码"
          prop="mouldCode"
          min-width="100"
          show-overflow-tooltip
          sortable
        />
        <!-- <el-table-column
          label="模具组合编码"
          prop="groupCode"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <!-- <el-table-column
          label="产品号"
          prop="productCode"
          min-width="100"
          show-overflow-tooltip
          sortable
        /> -->
        <el-table-column
          label="操作类型"
          prop="workKind"
          min-width="60"
          align="center"
          sortable
        >
          <template #default="scope">
            {{ useDictionary("MoldWorkKind", scope.row.workKind) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作时间"
          prop="workTime"
          min-width="100"
          show-overflow-tooltip
          align="center"
          sortable
        />
        <el-table-column
          label="操作人"
          prop="createdUserName"
          min-width="60"
          show-overflow-tooltip
          align="center"
          sortable
        />
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useRoute } from "vue-router";
import { filterOrder } from "@/utils/commonHooks";
defineOptions({
  name: "EAMEAMMODWORKRECORD",
  inheritAttrs: false,
});

const route = useRoute();

// 请求参数
const requestApi = "/eam/eammodworkrecord/queryByPage";

// 是否需要分页
const pagination = true;
const initParam = reactive({
  sort: {
    column: "",
    orderBy: "",
  },
});

// 初始化搜索参数数据
const searchParamInit = {
  workTimeSearch: [],
  workTimeStart: "",
  workTimeEnd: "",
  workKind: "",
  productCode: "",
  materialCode: "",
  materialName: "",
};

const deleteParam = {
  api: "/eam/eammodworkrecord/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);
// 排序
// 监听表格排序方法
function onTableSortChanged(column: any) {
  const { prop, order } = column;
  console.log(prop, order);

  initParam.sort = {
    column: prop,
    orderBy: filterOrder(order),
  };
}
// 监听页面 initParam 改化，重新获取表格数据
watch(() => initParam, getTableList, { deep: true });
onMounted(() => {
  searchParam.value.mouldCode = route.query.mod;
  getTableList();
});

function getworkTime(data: any) {
  if (data) {
    searchParam.value.workTimeStart = data[0];
    searchParam.value.workTimeEnd = data[1];
  } else {
    searchParam.value.workTimeStart = "";
    searchParam.value.workTimeEnd = "";
  }
}
</script>
